package cn.b.sky.mes.pp.controller;

import cn.b.sky.common.enums.ParameterEnums;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.mes.cm.model.*;
import cn.b.sky.mes.cm.service.CalendarService;
import cn.b.sky.mes.cm.service.ProcessService;
import cn.b.sky.mes.cm.service.WorkDayTypeService;
import cn.b.sky.mes.cm.service.WorkUnitService;
import cn.b.sky.mes.pp.enums.OperationTaskStatus;
import cn.b.sky.mes.pp.enums.ProductionControlStatusCode;
import cn.b.sky.mes.pp.model.OperationTask;
import cn.b.sky.mes.pp.model.OrderType;
import cn.b.sky.mes.pp.model.ProductionControl;
import cn.b.sky.mes.pp.model.condition.ProductionControlCondition;
import cn.b.sky.mes.pp.service.OperationTaskService;
import cn.b.sky.mes.pp.service.OrderTypeService;
import cn.b.sky.mes.pp.service.ProductionControlService;
import cn.b.sky.mes.pp.service.ProductionOrderPlanService;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.ParameterService;
import cn.b.sky.user.service.model.ECode;
import cn.b.sky.user.service.model.ParameterEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <strong>Title : OperationArrangementController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.pp.controller<br>
 * </strong> <strong>Description : </strong>工艺工序排产控制器<br>
 * <strong>Create on : 2016年1月15日 下午2:30:03<br>
 * </strong>
 * <p>
 *
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/operationArrangement/*")
public class OperationArrangementController extends SecurityController {

    private static final String CREATED_CONTROL_ARRANGEMENT_LIST = "pp/orderArrangement/uncompletedControl_list";

    private static final String OPERATION_ARRANGEMENT_LIST = "pp/orderArrangement/operationArrangement_list";

    private static final long PERDAY = 24 * 60 * 60 * 1000;

    @Autowired
    private CalendarService calendarService;

    @Autowired
    private WorkDayTypeService workDayTypeService;

    @Autowired
    private ProductionControlService productionControlService;

    @Autowired
    private ProcessService processService;

    @Autowired
    private WorkUnitService workUnitService;

    @Autowired
    private OperationTaskService operationTaskService;

    @Autowired
    private ParameterService parameterService;

    @Autowired
    OrderTypeService orderTypeService;

    @Autowired
    ECodeService eCodeService;

    @Autowired
    ProductionOrderPlanService orderPlanService;

    /**
     * <strong>Title : operationSchedulePage<br></strong>
     * <strong>Description : </strong>列表界面<br>
     * <strong>Create on : 2016年11月16日 上午8:55:59<br></strong>
     * <p>
     *
     * @param navTabId
     * @param model
     * @param startDate
     * @param endDate
     * @param orderType
     * @return String
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    @RequestMapping("unarrangedControl_list_page")
    public String operationSchedulePage(String navTabId, Model model, String startDate, String endDate,
                                        String orderType) {
        prepareArrangementPageModel(navTabId, model, startDate, endDate, orderType);
        return CREATED_CONTROL_ARRANGEMENT_LIST;
    }

    /**
     * <strong>Title : prepareArrangementPageModel<br></strong>
     * <strong>Description : </strong>数据绑定<br>
     * <strong>Create on : 2016年11月16日 上午8:55:55<br></strong>
     * <p>
     *
     * @param navTabId
     * @param model
     * @param startDate
     * @param endDate
     * @param orderType void
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    private void prepareArrangementPageModel(String navTabId, Model model, String startDate, String endDate,
                                             String orderType) {
        List<OrderType> orderTypes = orderTypeService.getAllOrderTypes(this.getSiteCode());
        List<ECode> eCodes = eCodeService.selectECodeList(ECodeEnums.PRODUCTIONCONTROL_STATUS.getCode(),
                SecurityContextUtil.getLanguageCode());
        model.addAttribute("orderTypes", JSON.toJSONString(orderTypes));
        model.addAttribute("eCodes", JSON.toJSONString(eCodes));
        bindModelAttributs(model, navTabId, startDate, endDate, orderType);
    }

    /**
     * <strong>Title : bindModelAttributs<br></strong>
     * <strong>Description : </strong>数据绑定<br>
     * <strong>Create on : 2016年11月16日 上午8:55:47<br></strong>
     * <p>
     *
     * @param model
     * @param navTabId
     * @param startDate
     * @param endDate
     * @param orderType void
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    private void bindModelAttributs(Model model, String navTabId, String startDate, String endDate, String orderType) {
        model.addAttribute(NAVTABID, navTabId);
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        model.addAttribute("orderType", orderType);
    }

    /**
     * <strong>Title : getCreatedControlsByCondition<br></strong>
     * <strong>Description : </strong>分页查询<br>
     * <strong>Create on : 2016年11月10日 下午1:33:45<br></strong>
     * <p>
     *
     * @param condition
     * @return DWZResult
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    @RequestMapping("getCreatedControlsByCondition")
    @ResponseBody
    public DWZResult getCreatedControlsByCondition(ProductionControlCondition condition) {
        condition.setSiteCode(getSiteCode());
        int count = productionControlService.getCreatedControlsCountByCondition(condition);
        if (count > 0) {
            condition.setData(productionControlService.getCreatedControlsByCondition(condition));
            condition.setTotalCount(count);
        }
        return ajaxDone(condition);
    }

    /**
     * <strong>Title : operationArrangementListPage<br></strong>
     * <strong>Description : </strong>工艺工序排产界面<br>
     * <strong>Create on : 2016年8月1日 上午10:12:53<br></strong>
     * <p>
     *
     * @param navTabId
     * @param startDate
     * @param endDate
     * @param orderNums
     * @param model
     * @param orderType
     * @return String
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    @RequestMapping("operationArrangement_list_page")
    public String operationArrangementListPage(Model model, String navTabId, String startDate, String endDate,
                                               String orderNums, String controlNums, String orderType) {
        // 分别记录已经存在的最小开始时间和最大的结束时间
        Date startDateTime = operationTaskService.getMinStartDateByOrderNums(getSiteCode(), orderNums.split(","));
        // 没有任务单则开始时间设置为下一天
        if (null == startDateTime || startDateTime.compareTo(new Date()) > 0) {
            startDateTime = new Date();
            startDateTime.setTime(startDateTime.getTime() + PERDAY);
        }
        // 甘特图开始时间
        model.addAttribute("start", new SimpleDateFormat("yyyy-MM-dd").format(startDateTime));
        Date endDateTime = operationTaskService.getMaxEndDateByOrderNums(getSiteCode(), orderNums.split(","));
        if (null != endDateTime) {
            // 甘特图结束时间
            model.addAttribute("end", new SimpleDateFormat("yyyy-MM-dd").format(endDateTime));
        }
        model.addAttribute("orderNums", orderNums);
        model.addAttribute("controlNums", controlNums);
        // 记录列表查询条件，返回时数据回填
        bindModelAttributs(model, navTabId, startDate, endDate, orderType);
        return OPERATION_ARRANGEMENT_LIST;
    }

    /**
     * <strong>Title : getControlProcessOperations<br></strong>
     * <strong>Description : </strong>查询管理号下的生产确认点工序相关数据<br>
     * <strong>Create on : 2016年11月16日 上午8:58:15<br></strong>
     * <p>
     *
     * @param orderNums
     * @param controlNums
     * @param startDate
     * @param endDate
     * @return Map<String   ,   Object>
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    @RequestMapping("getControlProcessOperations")
    @ResponseBody
    public Map<String, Object> getControlProcessOperations(String[] orderNums, String[] controlNums, String startDate,
                                                           String endDate) {
        Map<String, Object> map = new HashMap<String, Object>();
        // 获取订单的确认点生产工序
        Map<String, List<ProcessOperation>> processOperationMap = new HashMap<String, List<ProcessOperation>>();
        for (String orderNum : orderNums) {
            processOperationMap.put(orderNum, processService.getConfirmationProcessOperation(getSiteCode(), orderNum));
        }
        // 获取页面选中的管理号数据
        List<ProductionControl> controls = productionControlService.getProductionControlsByControlNums(getSiteCode(),
                controlNums);
        // 记录工作中心下某工序可选作业单元列表
        Map<String, List<WorkUnitJSON>> workUnitMap = new HashMap<String, List<WorkUnitJSON>>();
        // 涉及到的所有工作中心
        Set<String> workCenterCodes = new HashSet<String>();
        // 涉及到的所有工艺工序id
        Set<Integer> processOperationIds = new HashSet<Integer>();
        // 给管理号设置生产确认点
        setProcessOperations(processOperationMap, workUnitMap, workCenterCodes, processOperationIds, controls);
        // 涉及到的所有作业单元（查询历史数据）
        Set<String> workUnitCodeSet = new HashSet<String>();
        for (String key : workUnitMap.keySet()) {
            for (WorkUnitJSON workUnit : workUnitMap.get(key)) {
                workUnitCodeSet.add(workUnit.getWorkUnitCode());
            }
        }
        // 按照管理号、工序分组工序任务单
        Map<String, Map<String, List<OperationTask>>> operationTaskMap = mapOperationTasks(
                operationTaskService.getOperationTasksByControlNums(getSiteCode(), controlNums));
        map.put("controls", controls);
        map.put("workUnitMap", workUnitMap);
        map.put("operationTaskMap", operationTaskMap);
        map.put("workUnitCodeSet", workUnitCodeSet);
        // 查找相关作业单元时间范围内已经排产的工序任务单
        if (!workUnitCodeSet.isEmpty()) {
            Set<String> nums = operationTaskService.getControlNumsByWorkUnitCodes(getSiteCode(), workUnitCodeSet, startDate,
                    endDate);
            Set<String> otherOrderNums = new HashSet<String>();
            Set<String> otherControlNums = new HashSet<String>();
            for (String num : nums) {
                // 如果当前选中的管理号不包含这些数据则为影响当前订单排序的历史订单
                if (!ArrayUtils.contains(controlNums, num.split(";")[1])) {
                    otherOrderNums.add(num.split(";")[0]);
                    otherControlNums.add(num.split(";")[1]);
                }
            }
            map.put("otherOrderNums", otherOrderNums);
            map.put("otherControlNums", otherControlNums);
        }
        map.put("workCenterCodes", workCenterCodes);
        map.put("processOperationIds", processOperationIds);
        // 工作中心日历、产能相关数据
        setWorkCenterData(map, workCenterCodes.toArray(new String[]{}), processOperationIds.toArray(new Integer[]{}),
                startDate, endDate);
        // 甘特图label模板
        ParameterEntity entity = parameterService
                .getParameterEntityByCode(ParameterEnums.OPERATION_ARRANGEMENT_LABEL.getCode(), getSiteCode());
        if (null != entity) {
            map.put("label", entity.getDefaultValue());
        }
        return map;
    }

    /**
     * <strong>Title : getWorkCenterData<br></strong>
     * <strong>Description : </strong>获取工作中心日历、产能相关数据<br>
     * <strong>Create on : 2016年11月16日 上午10:08:12<br></strong>
     * <p>
     *
     * @param workCenterCodes
     * @param processOperationIds
     * @param startDate
     * @param endDate
     * @return Map<String   ,   Object>
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    @RequestMapping("loadWorkCenterData")
    @ResponseBody
    public Map<String, Object> loadWorkCenterData(String[] workCenterCodes, Integer[] processOperationIds,
                                                  String startDate, String endDate) {
        Map<String, Object> map = new HashMap<String, Object>();
        setWorkCenterData(map, workCenterCodes, processOperationIds, startDate, endDate);
        return map;
    }

    /**
     * <strong>Title : setProcessOperations<br></strong>
     * <strong>Description : </strong>设置生产管理号的生产确认点工艺工序<br>
     * <strong>Create on : 2016年9月27日 下午6:39:25<br></strong>
     * <p>
     * void
     *
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    private void setProcessOperations(Map<String, List<ProcessOperation>> processOperationMap,
                                      Map<String, List<WorkUnitJSON>> workUnitMap, Set<String> workCenterCodes, Set<Integer> processOperationIds,
                                      List<ProductionControl> controls) {
        // 记录工作中心下某作业单元类型的作业单元集
        Map<String, List<WorkUnitJSON>> map = new HashMap<String, List<WorkUnitJSON>>();
        for (ProductionControl control : controls) {
            setProcessOperations(processOperationMap, workUnitMap, workCenterCodes, processOperationIds, map, control);
        }
    }

    /**
     * <strong>Title : setProcessOperations<br></strong>
     * <strong>Description : </strong>设置单个管理号的生产确认点工序数据<br>
     * <strong>Create on : 2016年11月16日 上午9:14:55<br></strong>
     * <p>
     *
     * @param processOperationMap
     * @param workUnitMap
     * @param workCenterCodes
     * @param processOperationIds
     * @param map
     * @param control             void
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    private void setProcessOperations(Map<String, List<ProcessOperation>> processOperationMap,
                                      Map<String, List<WorkUnitJSON>> workUnitMap, Set<String> workCenterCodes, Set<Integer> processOperationIds,
                                      Map<String, List<WorkUnitJSON>> map, ProductionControl control) {
        workCenterCodes.add(control.getWorkCenterCode());
        // 将订单的生产确认点工序设置到管理号上
        control.setProcessOperations(processOperationMap.get(control.getProductionOrderNum()));
        //为空不处理
        if (CollectionUtils.isEmpty(control.getProcessOperations())) {
            return;
        }
        for (ProcessOperation operation : control.getProcessOperations()) {
            processOperationIds.add(operation.getId());
            if (!StringUtils.isEmpty(control.getWorkCenterCode()) && !StringUtils.isEmpty(operation.getWorkUnitTypeCode())) {
                List<WorkUnitJSON> workUnits = new LinkedList<WorkUnitJSON>();
                // 优化查询数据库的次数
                if (!map.containsKey(control.getWorkCenterCode() + "_" + operation.getWorkUnitTypeCode())) {
                    // 查询工作中心下指定作业单元类型的作业单元集
                    workUnits = workUnitService.selectWorkUnitByCenterAndType(getSiteCode(), control.getWorkCenterCode(),
                            operation.getWorkUnitTypeCode());
                    map.put(control.getWorkCenterCode() + "_" + operation.getWorkUnitTypeCode(), workUnits);
                } else {
                    workUnits = map.get(control.getWorkCenterCode() + "_" + operation.getWorkUnitTypeCode());
                }
                if (!workUnitMap.containsKey(control.getWorkCenterCode() + "_" + operation.getId())) {
                    workUnitMap.put(control.getWorkCenterCode() + "_" + operation.getId(), workUnits);
                }
            }
        }
    }

    /**
     * <strong>Title : mapOperationTasks<br></strong>
     * <strong>Description : </strong>将工序任务单按照管理号、工艺工序分组映射<br>
     * <strong>Create on : 2016年9月27日 下午6:36:41<br></strong>
     * <p>
     *
     * @param operationTasks
     * @return Map<String   ,   Map   <   String   ,   List   <   OperationTask>>>
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    private Map<String, Map<String, List<OperationTask>>> mapOperationTasks(List<OperationTask> operationTasks) {
        Map<String, Map<String, List<OperationTask>>> operationTaskMap = new HashMap<String, Map<String, List<OperationTask>>>();
        for (OperationTask task : operationTasks) {
            if (!operationTaskMap.containsKey(task.getProductionControlNum())) {
                operationTaskMap.put(task.getProductionControlNum(), new HashMap<String, List<OperationTask>>());
            }
            Map<String, List<OperationTask>> taskMap = operationTaskMap.get(task.getProductionControlNum());
            if (!taskMap.containsKey(task.getProcessOperationId().toString())) {
                taskMap.put(task.getProcessOperationId().toString(), new LinkedList<OperationTask>());
            }
            taskMap.get(task.getProcessOperationId().toString()).add(task);
        }
        return operationTaskMap;
    }

    /**
     * <strong>Title : setWorkCenterData<br></strong>
     * <strong>Description : </strong>设置工作中心相关数据<br>
     * <strong>Create on : 2016年9月27日 下午6:38:18<br></strong>
     * <p>
     *
     * @param map
     * @param workCenterCodes
     * @param processOperationIds
     * @param startDate
     * @param endDate             void
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    private void setWorkCenterData(Map<String, Object> map, String[] workCenterCodes, Integer[] processOperationIds,
                                   String startDate, String endDate) {
        // 组装工作中心日历相关数据
        Map<String, List<CalendarDay>> workCenterCalendarDaysMap = new HashMap<String, List<CalendarDay>>();
        List<WorkCenterCalendarDay> workCenterCalendarDays = calendarService.selectWorkCenterCalendarDays(getSiteCode(),
                workCenterCodes, startDate, endDate);
        for (WorkCenterCalendarDay calendarDay : workCenterCalendarDays) {
            if (!workCenterCalendarDaysMap.containsKey(calendarDay.getWorkCenterCode())) {
                workCenterCalendarDaysMap.put(calendarDay.getWorkCenterCode(), calendarDay.getCalendarDays());
            }
        }
        map.put("workCenterCalendarDaysMap", workCenterCalendarDaysMap);

        // 组装作业单元日历相关数据
        Map<String, List<CalendarDay>> workUnitCalendarDaysMap = new HashMap<String, List<CalendarDay>>();
        List<WorkUnitCalendarDay> workUnitCalendarDays = calendarService.selectWorkUnitCalendarDays(getSiteCode(),
                workCenterCodes, startDate, endDate);
        for (WorkUnitCalendarDay calendarDay : workUnitCalendarDays) {
            if (!workUnitCalendarDaysMap.containsKey(calendarDay.getWorkUnitCode())) {
                workUnitCalendarDaysMap.put(calendarDay.getWorkUnitCode(), calendarDay.getCalendarDays());
            }
        }
        map.put("workUnitCalendarDaysMap", workUnitCalendarDaysMap);

        // 组装日历类型的工作时段数据
        Map<String, List<WorkDayPeriod>> workDayPeriodsMap = new HashMap<String, List<WorkDayPeriod>>();
        List<WorkDayPeriod> workDayPeriods = workDayTypeService.selectWorkDayPeriods(getSiteCode());
        for (WorkDayPeriod workDayPeriod : workDayPeriods) {
            if (!workDayPeriodsMap.keySet().contains(workDayPeriod.getDayTypeCode())) {
                workDayPeriodsMap.put(workDayPeriod.getDayTypeCode(), new LinkedList<WorkDayPeriod>());
            }
            workDayPeriodsMap.get(workDayPeriod.getDayTypeCode()).add(workDayPeriod);
        }
        map.put("workDayPeriodsMap", workDayPeriodsMap);

        // 组装工艺工序的产能数据
        Map<String, List<ProcessOperationCapacity>> capacitiesMap = new HashMap<String, List<ProcessOperationCapacity>>();
        List<ProcessOperationCapacity> capacities = processService.getStandardCapacity(getSiteCode(), processOperationIds);
        for (ProcessOperationCapacity capacity : capacities) {
            if (!capacitiesMap.containsKey(capacity.getProcessOperationId())) {
                capacitiesMap.put(capacity.getProcessOperationId(), new LinkedList<ProcessOperationCapacity>());
            }
            capacitiesMap.get(capacity.getProcessOperationId()).add(capacity);
        }
        map.put("capacitiesMap", capacitiesMap);
    }

    /**
     * <strong>Title : saveOperationTasks<br></strong>
     * <strong>Description : </strong>保存工序任务单<br>
     * <strong>Create on : 2016年8月22日 下午5:11:51<br></strong>
     * <p>
     *
     * @param operationTasks
     * @return DWZResult
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    @RequestMapping("saveOperationTasks")
    @ResponseBody
    public DWZResult saveOperationTasks(String operationTasks) {
        List<OperationTask> tasks = JSONArray.parseArray(operationTasks, OperationTask.class);
        for (OperationTask task : tasks) {
            setCreateInfo(task);
            if (StringUtils.isEmpty(task.getStatusECode())) {
                task.setStatusECode(OperationTaskStatus.CREATE.getValue());
            }
        }
        operationTaskService.saveOperationTasks(tasks);
        return ajaxDone(tasks);
    }

    /**
     * <strong>Title : issueOperationTasks<br></strong>
     * <strong>Description : </strong>下达工序任务单<br>
     * <strong>Create on : 2016年8月22日 下午5:12:08<br></strong>
     * <p>
     *
     * @param operationTaskNums
     * @return DWZResult
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    @RequestMapping("issueOperationTasks")
    @ResponseBody
    public DWZResult issueOperationTasks(String[] operationTaskNums) {
        return ajaxDone(operateOperationTasks(operationTaskNums, ProductionControlStatusCode.ISSUED));
    }

    /**
     * <strong>Title : releaseOperationTasks<br></strong>
     * <strong>Description : </strong>撤销下达工序任务单<br>
     * <strong>Create on : 2016年8月22日 下午5:12:22<br></strong>
     * <p>
     *
     * @param operationTaskNums
     * @return DWZResult
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    @RequestMapping("releaseOperationTasks")
    @ResponseBody
    public DWZResult releaseOperationTasks(String[] operationTaskNums) {
        return ajaxDone(operateOperationTasks(operationTaskNums, ProductionControlStatusCode.CREATE));
    }

    /**
     * <strong>Title : operateOperationTasks<br></strong>
     * <strong>Description : </strong>工序任务单下达/撤销操作<br>
     * <strong>Create on : 2016年11月22日 上午9:29:22<br></strong>
     * <p>
     *
     * @param operationTaskNums
     * @param statusCode
     * @return List<BatchJobReport>
     * @throws
     * @author dapeng.li<br>
     * @version <strong>v1.0.0</strong><br>
     * <br>
     * <strong>修改历史:</strong><br>
     * 修改人 | 修改日期 | 修改描述<br>
     * -------------------------------------------<br>
     * <br>
     * <br>
     */
    private List<BatchJobReport> operateOperationTasks(String[] operationTaskNums,
                                                       ProductionControlStatusCode statusCode) {
        Map<String, Set<String>> map = new HashMap<String, Set<String>>();
        for (String num : operationTaskNums) {
            if (!map.containsKey(num.split(";")[0])) {
                map.put(num.split(";")[0], new HashSet<String>());
            }
            map.get(num.split(";")[0]).add(num.split(";")[1]);
        }
        List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
        int failures = 0;
        String message = null;
        for (String key : map.keySet()) {
            Set<String> operationTaskNumSet = map.get(key);
            int success = 0;
            for (String operationTaskNum : operationTaskNumSet) {
                if (ProductionControlStatusCode.CREATE.equals(statusCode)) {
                    message = operationTaskService.revocationOperationTask(this.getSiteCode(), operationTaskNum);
                } else {
                    message = operationTaskService.releaseOperationTask(this.getSiteCode(), operationTaskNum);
                }
                boolean flag = true;
                if (!StringUtils.isBlank(message)) {
                    failures += 1;
                    flag = false;
                    message = getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), message);
                } else {
                    success += 1;
                }
                BatchJobReport report = new BatchJobReport(operationTaskNum, message, flag);
                reports.add(report);
            }
            if (success == operationTaskNumSet.size()) {
                ProductionControl control = new ProductionControl();
                control.setSiteCode(getSiteCode());
                control.setProductionControlNum(key);
                control.setStatuseCode(statusCode.getValue());
                setCreateInfo(control);
                productionControlService.updateProductionControl(control);
            }
        }
        if (failures == 0) {
            return null;
        } else {
            return reports;
        }
    }

}
